package com.wiloon.java8x.thread.executor;

/**
 * Created by Roy on 2015/11/13;
 */

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * newFixedThreadPool
 */
public class ExecutorTest {
    public static void main(String args[]) {
        Random random = new Random();

        ExecutorService executor = Executors.newFixedThreadPool(5);

        int waitTime = 500;
        for (int i = 0; i < 10; i++) {
            String name = "Thread " + i;
            int time = random.nextInt(1000);
            waitTime += time;
            Runnable runner = new ExecutorThread(name, time);
            System.out.println("add:" + name + " / " + time);
            executor.execute(runner);
        }
        try {
            Thread.sleep(waitTime);
            executor.shutdown();
            executor.awaitTermination(waitTime, TimeUnit.MILLISECONDS);
        } catch (InterruptedException ignored) {
        }
    }
}